package com.hhs.storage.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.*;
import com.hhs.storage.util.FileUpload;
import com.hhs.storage.util.StringUtil;
import com.hhs.storage.util.SysUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chx
 * @since 2024-10-19
 */
@RestController
@RequestMapping("xmclazz")
@Slf4j
public class XmClazzController {
    @Resource
    private XmClazzService xmClazzService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private BaseUserService baseUserService;
    @Resource
    private XmSchoolService xmSchoolService;
    @Resource
    private XmUserClazzService xmUserClazzService;
    @Resource
    private XmUserWorkService xmUserWorkService;
    @Value("${upload.folderName}")
    private String folderName;

    @RequestMapping("edit")
    public Map<String,Object> edit(XmClazz xmClazz){
        BaseUser byId = baseUserService.getById(SysUtil.getLoginUserId(request));
        if(StringUtil.iszNull(xmClazz.getId())){
            if(!StringUtil.iszNull(byId.getSchoolId())){
                xmClazz.setSchoolId(byId.getSchoolId());
            }
            xmClazz.setCreateTime(LocalDateTime.now());
        }
        xmClazzService.saveOrUpdate(xmClazz);
        return ResponseInfo.success();
    }
    @RequestMapping("table")
    public Map<String,Object> table(InputParam inputParam){
        Map<String,Object> map=new HashMap<>();
        //得到登录者所在的学校
        BaseUser byIdUser = baseUserService.getById(SysUtil.getLoginUserId(request));
        QueryWrapper qw=new QueryWrapper();
        if(!"2".equals(byIdUser.getId())) {
            qw.eq("school_id", byIdUser.getSchoolId());
        }
        inputParam.mapParamsNoTail("name");
        if(!StringUtil.iszNull(inputParam.getName())){
            qw.like("clazz_name", inputParam.getName());
        }
        if(!StringUtil.iszNull(inputParam.getOrderBy())){
            if(inputParam.getAsc()==true){
                qw.orderByAsc(inputParam.getOrderBy());
            }else {
                qw.orderByDesc(inputParam.getOrderBy());
            }
        }
        Page<XmClazz> mp = xmClazzService.page(new Page(inputParam.getPage(), inputParam.getSize()),qw);
        //学校名处理
        for (XmClazz xmc:mp.getRecords()){
            XmSchool byId = xmSchoolService.getById(xmc.getSchoolId());
            if(byId != null){
                xmc.setSchoolName(byId.getName());
            }
            QueryWrapper xqw=new QueryWrapper();
            xqw.eq("clazz_id",xmc.getId());
            xqw.inSql("user_id","SELECT a.user_id_ FROM base_role_user a LEFT JOIN base_role b ON a.`role_id_`=b.`id_` WHERE flag_='student'");
            int count = xmUserClazzService.count(xqw);
            xmc.setNowNumber(count);
        }
        map.put("code",1);
        map.put("data",mp.getRecords());
        map.put("total",mp.getTotal());
        return map;
    }
    @RequestMapping("dele")
    public Map<String,Object> dele(XmClazz xmClazz){
        if(StringUtil.iszNull(xmClazz.getId())){
            return ResponseInfo.error("没有id,参数错误");
        }
        xmClazzService.removeById(xmClazz.getId());
        return ResponseInfo.success();
    }
    @RequestMapping("addStu")
    public Map<String,Object> addStu(InputParam inputParam){
        if(StringUtil.iszNull(inputParam.getId())){
            return ResponseInfo.error("没有id,参数错误");
        }
        if(StringUtil.iszNull(inputParam.getIds())){
            return ResponseInfo.error("没有ids,参数错误");
        }
        return xmClazzService.addStu(inputParam);
    }

    @RequestMapping("cancelStu")
    public Map<String,Object> cancelStu(InputParam inputParam){
        if(StringUtil.iszNull(inputParam.getId())){
            return ResponseInfo.error("没有id,参数错误");
        }
        if(StringUtil.iszNull(inputParam.getIds())){
            return ResponseInfo.error("没有ids,参数错误");
        }
        return xmClazzService.cancelStu(inputParam);
    }

    @RequestMapping("getMyClazz")
    public Map<String,Object> getMyClazz(InputParam inputParam){
        if(StringUtil.iszNull(inputParam.getUserId())){
            return ResponseInfo.error("没有useid,参数错误");
        }
        QueryWrapper qw=new QueryWrapper();
        qw.eq("user_id",inputParam.getUserId());
        List<XmUserClazz> list = xmUserClazzService.list(qw);
        for(XmUserClazz xmUserClazz:list){
            XmClazz byId = xmClazzService.getById(xmUserClazz.getClazzId());
            //查询班级学生数
            InputParam myInputParam=new InputParam();
            myInputParam.setTargetId(byId.getId());
            myInputParam.setRelateType("student");
            int count = xmUserClazzService.getUserCount(myInputParam);
            xmUserClazz.setUserNumber(count);
            xmUserClazz.setClazz(byId);
        }
        return ResponseInfo.success(list);
    }
    @RequestMapping("bindClazz")
    public Map<String,Object> bindClazz(String userId,String clazzIds){
        if(StringUtil.iszNull(userId)){
            return ResponseInfo.error("没有useid,参数错误");
        }
        QueryWrapper qw=new QueryWrapper();
        qw.eq("user_id",userId);
        if(StringUtil.iszNull(clazzIds)){
            xmUserClazzService.remove(qw);
        }else{
            String[] ids=clazzIds.split(",");
            xmUserClazzService.remove(qw);
            for(String id:ids){
                XmUserClazz xmUserClazz = new XmUserClazz();
                xmUserClazz.setUserId(userId);
                xmUserClazz.setClazzId(id);
                xmUserClazzService.save(xmUserClazz);
            }
        }
        return ResponseInfo.success();
    }


}

